package org.s3.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.s3.project.common.JwtUtils;
import org.s3.project.common.R;
import org.s3.project.entity.Dish;
import org.s3.project.entity.User;
import org.s3.project.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:29
 */
@CrossOrigin(origins = "*")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    //查询菜品
    @GetMapping("/getDishSelect")
    public List<Dish> getDishSelect(){
        return dishService.getDishSelect();
    }
    //只显示评分最高的三个菜品
    @GetMapping("getDishSelectByInventory")
    List<Dish> getDishSelectByInventory(){

        return dishService.getDishSelectByInventory();
    }

    // 查询商家所有菜品
    @GetMapping("/selectAllByBusinessId")
    public List<Dish> selectAllByBusinessId(Integer businessId){
        return dishService.selectAllByBusinessId(businessId);
    }
    //修改
    @PostMapping("/updateDish")
    public int updateDish(@RequestBody Dish dish){
        return dishService.updateDish(dish);
    }
    //添加
    @PostMapping("/insertDish")
    public int insertDish(@RequestBody Dish dish){
        System.out.println("dish = " + dish);
        return dishService.insertDish(dish);
    }
    //详情
    @GetMapping("getDishId/{id}")
    public Dish getDishId(@PathVariable("id") int id){
        return dishService.getDishId(id);
    }
    //通过商家id查询菜品
    @GetMapping("/selectDishByMerchant/{id}")
    public List<Dish>selectDishByMerchant(@PathVariable("id") int id){
        return dishService.selectDishByMerchant(id);
    }

    /**
     * 分页查询菜品信息
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @return 菜品信息列表
     */
    @GetMapping("showDish/{page}")
    public R selectDishPage(@PathVariable("page")int page, Integer pageSize,
                            @RequestParam(required = false) Integer id,
                            @RequestParam(required = false) String Dname,
                            @RequestParam(required = false) String create_time,
                            @RequestParam(required = false) String create_timeOne,
                            @RequestParam(required = false) Integer status){
        PageHelper.startPage(page,pageSize);//设置分页
        List<Dish> dishList= this.dishService.selectDishByMerchants(id,Dname,create_time,create_timeOne,status);

        PageInfo<Dish> dishPageList = new PageInfo<>(dishList);//执行连表查询
        return new R().add("dishPageList",dishPageList);
    }

    /**
     * 修改菜品状态
     * @param dish 菜品对象
     * @param token 管理员token
     * @return 修改结果
     */
    @GetMapping("updateDishStatus")
    public int updateDishStatus(Dish dish,@RequestHeader("token") String token) {
        Integer adminId= JwtUtils.getJwtToken(token,"adminId");
        dish.setUpdate_user(adminId);//设置修改人id为当前登录用户的id
        return dishService.updateDishStatus(dish);
    }

    /**
     * 批量修改菜品状态
     * @param ids 菜品id数组
     * @param dish 菜品对象
     * @param token 管理员token
     * @return 修改结果
     */
    @GetMapping("updateDishStatusIds")
    public int updateDishStatusIds(int[] ids,Dish dish,@RequestHeader("token") String token) {
        Integer dishId= JwtUtils.getJwtToken(token,"adminId");
        dish.setUpdate_user(dishId);//设置修改人id为当前登录用户的id
        return dishService.updateDishStatusIds(ids,dish);
    }


    /**
     * 分页查询审核菜品信息
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @param Dname 菜品名称
     * @param Mname 商家名称
     * @param create_time 创建时间
     * @param create_timeOne 创建时间
     * @return 审核菜品信息列表
     */
    @GetMapping("showDishCheck/{page}")
    public R selectDishPageCheck(@PathVariable("page")int page, Integer pageSize,
                            @RequestParam(required = false) String Dname,
                            @RequestParam(required = false) String Mname,
                            @RequestParam(required = false) String create_time,
                            @RequestParam(required = false) String create_timeOne
                           ){
        PageHelper.startPage(page,pageSize);//设置分页
        List<Dish> dishList= this.dishService.selectDishByMerchantChecks(Dname,Mname,create_time,create_timeOne);

        PageInfo<Dish> dishPageList = new PageInfo<>(dishList);//执行连表查询
        return new R().add("dishCheckPageList",dishPageList);
    }
    //查询菜品分类
    @GetMapping("getDishByCategoryId/{id}")
    public List<Dish> getDishByCategoryId(@PathVariable("id") int id){
        return dishService.getDishByCategoryId(id);
    }
    //模糊查询菜品
    @GetMapping("searchDish/{name}")
    public List<Dish> getDishByDishName(@PathVariable("name") String name){
        return dishService.getDishByDishName(name);
    }
    //通过菜品id查询商家
    @GetMapping("/selectBusinessId/{id}/{category_id}")
    public List<Dish>selectBusinessId(@PathVariable("id") int id,@PathVariable("category_id") int category_id){
        return dishService.selectBusinessId(id,category_id);
    }

}
